<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>管理员管理</title>
<!-- 引入easyui框架 -->
<link rel="stylesheet" type="text/css"
	href="../js/jquery-easyui-1.7.0/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="../js/jquery-easyui-1.7.0/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="../js/jquery-easyui-1.7.0/themes/color.css">
<script type="text/javascript"
	src="../js/jquery-easyui-1.7.0/jquery.min.js"></script>
<script type="text/javascript"
	src="../js/jquery-easyui-1.7.0/jquery.easyui.min.js"></script>
</head>
<body>


    <!-- 设计一个数据表格用于列表显示管理员信息 -->
    <!-- 数据表格list  start  -->
	<table id="admin_dg" title="管理员信息" class="easyui-datagrid"
		style="width: 100%; height: 600px" url="/elmweb/sys/admin/search"
		toolbar="#admin_toolbar" pagination="true" rownumbers="true"
		fitColumns="true" fit="true" singleSelect="true">
		<thead>
			<tr>
				<th field="adminId" width="50">编号</th>
				<th field="adminName" width="50">帐号</th>
				<!-- 使用easyui格式化函数解决数据的显示问题 -->
				<th field="adminImage" formatter="showImage" width="50">密码</th>
				<th field="password" formatter="showPassword" width="50">密码</th>
			</tr>
		</thead>
	</table>
	<div id="admin_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newAdmin()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editAdmin()">修改</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyAdmin()">删除</a>
	</div>
	<!-- 数据表格list  start  -->
	
	<!-- 数据新增对话框  start -->
	<div id="admin_dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
		closed="true" buttons="#admin_dlg-buttons">
		<div class="ftitle">管理员</div>
		<form id="admin_fm" enctype="multipart/form-data" method="post">
			<div class="fitem">
				<label>管理员帐号:</label>
				<input id="adminName" name="adminName" class="easyui-textbox" required="true">
			</div>
			
			<div class="fitem">
				<label>管理员密码:</label>
				<input id="password" name="password" class="easyui-passwordbox" required="true">
			</div>
			
			<div class="fitem">
				<label>管理员头像:</label>
				<input name="file1" class="easyui-filebox">
			</div>
			
		</form>
	</div>
	<div id="admin_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveAdmin()">保存</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#admin_dlg').dialog('close')">取消</a>
	</div>
	
	<!-- 数据新增对话框  end -->
	
	
	<script type="text/javascript">
	
	   function newAdmin(){
		   //弹出新增对话框
		   $('#admin_dlg').dialog('open').dialog('setTitle','新增管理员');
		   //清空历史表单记录
			$('#admin_fm').form('clear');
			$('#adminName').textbox('enable');
			$('#password').textbox('enable');
		   //配置新增管理员的地址
			url = '/elmweb/sys/admin/add';
	   }
	   
	   function editAdmin(){
		   //从数据表格中获取选定的要修改的一行数据
		   var row = $('#admin_dg').datagrid('getSelected');
		   $('#admin_fm').form('clear');
		   
		   if (row){
		   	$('#admin_dlg').dialog('open').dialog('setTitle','修改管理员');
		   	//把选择的数据填充会数据修改表单
		   	$('#admin_fm').form('load',row);
		    $('#adminName').textbox('disable');
		    $('#password').textbox('disable');
		   	//api
		   	url = '/elmweb/sys/admin/edit?adminId='+row.adminId;
		   }else{
			   $.messager.show({
					title: 'Error',
					msg: '请选择一行数据'
				}); 
		   }
	   }
	   
	   function saveAdmin(){
		   $('#admin_fm').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					var result = eval('('+result+')');
					if (result.errorMsg){
						$.messager.show({
							title: 'Error',
							msg: result.errorMsg
						});
					} else {
						$.messager.show({
							title: 'Error',
							msg: "操作成功"
						});
						$('#admin_dlg').dialog('close');		// close the dialog
						$('#admin_dg').datagrid('reload');	// reload the user data
					}
				}
			});
	   }
	   
	   function destroyAdmin(){
			var row = $('#admin_dg').datagrid('getSelected');
			if (row){
				$.messager.confirm('Confirm','请确认删除管理员?',function(r){
					if (r){
						$.post('/elmweb/sys/admin/del',{adminId:row.adminId},function(result){
							if (result.success){
								$('#admin_dg').datagrid('reload');	// reload the user data
							} else {
								$.messager.show({	// show error message
									title: 'Error',
									msg: result.errorMsg
								});
							}
						},'json');
					}
				});
			}
		}
	
	   function showPassword(value,row,index){
		      return "<font color='red'>密码不可查看</font>";
	   }
	   
	   function showImage(value,row,index){
		   return "<img height='50px' src='../"+value+"'>";
	   }
	   
	</script>
	
</body>
</html>